package org.example.edusmart.controller.Public;

import org.example.edusmart.request.Public.SearchSourcePojo;
import org.example.edusmart.service.PublicService;
import org.example.edusmart.utils.Result;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RequestMapping("/public")
@RestController
public class SearchSourceController {

    @Autowired
    private PublicService publicService;

    @PostMapping("/SearchSource/")
    @PreAuthorize("hasAuthority('public_permission')")
    public Result SearchSource(@RequestBody @NotNull SearchSourcePojo searchSourcePojo){
        if ("".equals(searchSourcePojo.getKeyword())){
            return new Result(404,"请输入关键词");
        }
        List<HashMap<String,Object>> data = publicService.SearchSource(searchSourcePojo);
        if (data.isEmpty()){
            return new Result(404,"换个关键词试试");
        }
        return new Result(data,200,"搜索成功");
    }
}
